package com.yuelao.proxy;

import com.yuelao.proxy.jdkproxy.ProxyFactory;
import com.yuelao.proxy.tart.IUserDao;
import com.yuelao.proxy.tart.UserDao;

import java.io.*;

/**
 * @author FLY
 * @version v1.0
 * @project yuelao-server
 * @date 2017/9/26 14:30
 * @修改记录 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 */
public class ProxyTest {
    /**
     * JDK
     *
     * @param args
     */
    public static void main(String[] args) {
        // JDK
        UserDao hello = new UserDao();
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(hello);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            //读取二进制数据并转换成对象
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Object obj = null;
        try {
            obj = ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        IUserDao dao = (IUserDao) new ProxyFactory(obj).getProxyInstance();
        dao.hello();
        // Cglib
//        UserDao userDao = new UserDao();
//        UserDao userDaos = (UserDao) new CglibProxy(userDao).getProxyInstance();
//        userDaos.hello();
    }

}
